package com.weibo.survey.controller;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.weibo.survey.domain.Survey;
import com.weibo.survey.domain.User;
import com.weibo.survey.service.SurveyService;

@Controller
@Scope("prototype")
public class SurveyAction extends BaseAction<Survey> implements UserAware, ServletContextAware {
	private static final long serialVersionUID = 1L;
	private static final String DESIGNSURVEY = "designSurvey";
	private static final String EDITSURVEY = "editSurvey";
	private static final String MYSURVEY = "mySurvey";
	private static final String TOLOGIN = "toLogin";
	private static final String LISTSURVEY = "listSurveys";
	private static final String TOLOGOPAGE = "toLogoPage";

	private User user;

	private List<Survey> mySurveys;

	private ServletContext sc;

	private int sid;

	private File logo;

	private String logoFileName;

	@Resource(name = "surveyService")
	private SurveyService surveyService;

	// 新建调查
	public String newSurvey() {
		model = surveyService.newSurvey(user);
		return EDITSURVEY;
	}

	// 更新调查
	public String updateSurvey() {
		sid = model.getId();
		model.setUser(user);
		surveyService.updateEntity(model);
		return LISTSURVEY;
	}

	// 删除调查
	public String deleteSurvey() {
		surveyService.deleteSurvey(sid);
		return LISTSURVEY;
	}

	// 我的调查
	public String listSurveys() {
		if (user != null) {
			mySurveys = surveyService.findAllSurvey(user);
			return MYSURVEY;
		} else {
			String msg = "您尚未登录，请登录后重试";
			ActionContext.getContext().getValueStack().set("msg", msg);
			return TOLOGIN;
		}
	}

	// 编辑调查
	public String editSurvey() {
		model = surveyService.getSurveyWithChildren(sid);
		return EDITSURVEY;
	}

	// 设计调查
	public String designSurvey() {
		model = surveyService.getSurveyWithChildren(sid);
		return DESIGNSURVEY;
	}

	// 打开/关闭调查
	public String toggleSurvey() {
		surveyService.toggleSurvey(sid);
		return LISTSURVEY;
	}

	public String toLogoPage() {
		return TOLOGOPAGE;
	}

	public String addLogo() {
		String dirPath = sc.getRealPath("/upload");
		long nonTime = System.nanoTime();
		String extension = logoFileName.substring(logoFileName.lastIndexOf("."));
		logo.renameTo(new File(dirPath, nonTime + extension));
		surveyService.updateLogoPhoto(sid, "/upload/" + nonTime + extension);
		return LISTSURVEY;
	}

	public String clearSurvey() {
		surveyService.clearSurvey(sid);
		return "listSurveys";
	}

	public List<Survey> getMySurveys() {
		return mySurveys;
	}

	public void setMySurveys(List<Survey> mySurveys) {
		this.mySurveys = mySurveys;
	}

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	@Override
	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public void setServletContext(ServletContext sc) {
		this.sc = sc;
	}

	public void setLogo(File logo) {
		this.logo = logo;
	}

	public void setLogoFileName(String logoFileName) {
		this.logoFileName = logoFileName;
	}

}
